#include "DateTimeUtils.h"

/**
 * 格式 FORMAT 控制着输出格式. 仅当选项指定为全球时间时本格式才有效。 分别解释如下:
 * %%     文本的 %
 * %a     当前区域的星期几的简写 (Sun..Sat)
 * %A     当前区域的星期几的全称 (不同长度) (Sunday..Saturday)
 * %b     当前区域的月份的简写 (Jan..Dec)
 * %B     当前区域的月份的全称(变长) (January..December)
 * %c     当前区域的日期和时间 (Sat Nov 04 12:02:33 EST 1989)
 * %d     (月份中的)几号(用两位表示) (01..31)
 * %D     日期(按照 月/日期/年 格式显示) (mm/dd/yy)
 * %e     (月份中的)几号(去零表示) ( 1..31)
 * %h     同 %b
 * %H     小时(按 24 小时制显示，用两位表示) (00..23)
 * %I     小时(按 12 小时制显示，用两位表示) (01..12)
 * %j     (一年中的)第几天(用三位表示) (001..366)
 * %k     小时(按 24 小时制显示，去零显示) ( 0..23)
 * %l     小时(按 12 小时制显示，去零表示) ( 1..12)
 * %m     月份(用两位表示) (01..12)
 * %M     分钟数(用两位表示) (00..59)
 * %n     换行
 * %p     当前时间是上午 AM 还是下午 PM
 * %r     时间,按 12 小时制显示 (hh:mm:ss [A/P]M)
 * %s     从 1970年1月1日0点0分0秒到现在历经的秒数 (GNU扩充)
 * %S     秒数(用两位表示)(00..60)
 * %t     水平方向的 tab 制表符
 * %T     时间,按 24 小时制显示(hh:mm:ss)
 * %U     (一年中的)第几个星期，以星期天作为一周的开始(用两位表示) (00..53)
 * %V     (一年中的)第几个星期，以星期一作为一周的开始(用两位表示) (01..52)
 * %w     用数字表示星期几 (0..6); 0 代表星期天
 * %W     (一年中的)第几个星期，以星期一作为一周的开始(用两位表示) (00..53)
 * %x     按照 (mm/dd/yy) 格式显示当前日期
 * %X     按照 (%H:%M:%S) 格式显示当前时间
 * %y     年的后两位数字 (00..99)
 * %Y     年(用 4 位表示) (1970...)
 * %z     按照 RFC-822 中指定的数字时区显示(如, -0500) (为非标准扩充)
 * %Z     时区(例如, EDT (美国东部时区)), 如果不能决定是哪个时区则为空
 * 默认情况下,用 0 填充数据的空缺部分.  GNU 的 date 命令能分辨在 `%'和数字指示之间的以下修改.
 *         `-' (连接号) 不进行填充 `_' (下划线) 用空格进行填充
 */

// 初始化 DateTime 结构体为某种时间
struct tm* makeDateTime(DateTime *datetime, time_t timestamp, bool useLocalTime) {
    struct tm *_timestruct = NULL;

    if (useLocalTime) {
        _timestruct = localtime(&timestamp);
    } else {
        _timestruct = gmtime(&timestamp);
    }

    if (datetime != NULL) {
        datetime->timestamp = timestamp;
        memcpy(&datetime->timestruct, _timestruct, sizeof(datetime->timestruct));
    }

    return _timestruct;
}

// ---------------------------------------------------------------- 
/**
 * The <time.h> header shall declare the tm structure, which shall include at least the following members:
 * 
 *     int    tm_sec   Seconds [0,60].
 *     int    tm_min   Minutes [0,59].
 *     int    tm_hour  Hour [0,23].
 *     int    tm_mday  Day of month [1,31].
 *     int    tm_mon   Month of year [0,11].
 *     int    tm_year  Years since 1900.
 *     int    tm_wday  Day of week [0,6] (Sunday =0).
 *     int    tm_yday  Day of year [0,365].
 *     int    tm_isdst Daylight Savings flag.
 */

// 日历时间: 由协调世界时(UTC)时间计算历来的秒数
// 日历时间: 1970年1月1日 00:00:00 开始到现在的秒数
time_t currentCalendarTime() {
    return time(NULL);
}

// GMT(Greenwich Mean Time)格林尼治标准时
struct tm* createGreenwichMeanDateTime() {
    time_t timestamp = currentCalendarTime();
    return gmtime(&timestamp);
}

// UTC(Coordinated Universal Time)协调世界时，
// 由于目前无法提供闰秒功能，与格林尼治标准时实现相同
struct tm* currentCoordinatedUniversalDateTime() {
    time_t timestamp = currentCalendarTime();
    return gmtime(&timestamp);
}

// ----------------------------------------------------------------

// GMT(Greenwich Mean Time)格林尼治标准时
struct tm* createCalendarDateTime(DateTime *datetime) {
    time_t timestamp = currentCalendarTime();
    struct tm *grrenwich = createGreenwichMeanDateTime();
    if (datetime != NULL) {
        datetime->timestamp = timestamp;
        memcpy(&datetime->timestruct, grrenwich, sizeof(datetime->timestruct));
    }
    return grrenwich;
}
int currentCalendarDateTimeYear(DateTime *datetime) {
    struct tm *grrenwich = NULL;
    if (datetime != NULL)
        grrenwich = &datetime->timestruct;
    else
        grrenwich = createGreenwichMeanDateTime(NULL);
    return grrenwich->tm_year;
}
int currentCalendarDateTimeYearMonth(DateTime *datetime) {
    struct tm *grrenwich = NULL;
    if (datetime != NULL)
        grrenwich = &datetime->timestruct;
    else
        grrenwich = createGreenwichMeanDateTime(NULL);
    return grrenwich->tm_mon;
}
int currentCalendarDateTimeYearMonthDay(DateTime *datetime) {
    struct tm *grrenwich = NULL;
    if (datetime != NULL)
        grrenwich = &datetime->timestruct;
    else
        grrenwich = createGreenwichMeanDateTime(NULL);
    return grrenwich->tm_mday;
}
int currentCalendarDateTimeYearMonthDayHour(DateTime *datetime) {
    struct tm *grrenwich = NULL;
    if (datetime != NULL)
        grrenwich = &datetime->timestruct;
    else
        grrenwich = createGreenwichMeanDateTime(NULL);
    return grrenwich->tm_hour;
}
int currentCalendarDateTimeYearMonthDayMinute(DateTime *datetime) {
    struct tm *grrenwich = NULL;
    if (datetime != NULL)
        grrenwich = &datetime->timestruct;
    else
        grrenwich = createGreenwichMeanDateTime(NULL);
    return grrenwich->tm_min;
}
int currentCalendarDateTimeYearMonthDaySecond(DateTime *datetime) {
    struct tm *grrenwich = NULL;
    if (datetime != NULL)
        grrenwich = &datetime->timestruct;
    else
        grrenwich = createGreenwichMeanDateTime(NULL);
    return grrenwich->tm_sec;
}

// ----------------------------------------------------------------
// Local Time 本地时间 - 经由 日历时间 计算，时区自动调整(+8)
struct tm* createLocalDateTime(DateTime *datetime) {
    time_t timestamp = currentCalendarTime();
    struct tm *_localtime = localtime(&timestamp);
    if (datetime != NULL) {
        datetime->timestamp = timestamp;
        memcpy(&datetime->timestruct, _localtime, sizeof(datetime->timestruct));
    }
    return _localtime;
}
int currentLocalDateTimeYear(DateTime *datetime) {
    struct tm *localtime = NULL;
    if (datetime != NULL) 
        localtime = &datetime->timestruct;
    else
        localtime = createLocalDateTime(NULL);
    return localtime->tm_year + 1900;
}
int currentLocalDateTimeYearMonth(DateTime *datetime) {
    struct tm *localtime = NULL;
    if (datetime != NULL) 
        localtime = &datetime->timestruct;
    else
        localtime = createLocalDateTime(NULL);
    return localtime->tm_mon + 1;
}
int currentLocalDateTimeYearMonthDay(DateTime *datetime) {
    struct tm *localtime = NULL;
    if (datetime != NULL) 
        localtime = &datetime->timestruct;
    else
        localtime = createLocalDateTime(NULL);
    return localtime->tm_mday;
}
int currentLocalDateTimeYearMonthDayHour(DateTime *datetime) {
    struct tm *localtime = NULL;
    if (datetime != NULL) 
        localtime = &datetime->timestruct;
    else
        localtime = createLocalDateTime(NULL);
    return localtime->tm_hour;
}
int currentLocalDateTimeYearMonthDayMinute(DateTime *datetime) {
    struct tm *localtime = NULL;
    if (datetime != NULL) 
        localtime = &datetime->timestruct;
    else
        localtime = createLocalDateTime(NULL);
    return localtime->tm_min;
}
int currentLocalDateTimeYearMonthDaySecond(DateTime *datetime) {
    struct tm *localtime = NULL;
    if (datetime != NULL) 
        localtime = &datetime->timestruct;
    else
        localtime = createLocalDateTime(NULL);
    return localtime->tm_sec;
}

/*
int	main(int argc, char **argv)
{
    printf("Hello World\n");
    struct tm *timeinfo = createLocalDateTime();
    
    char buffer[1000];
    int charlength = strftime(buffer, sizeof(buffer), "%c %A %B", timeinfo);

    printf("%s - (字符数: %d)\n", buffer, charlength);

// ----------------------------------------------------------------
// 格林尼治标准时
    time_t timestamp = 0;
    time(&timestamp);
    struct tm *timep = gmtime(&timestamp);  // 
    printf("time->tm_year: %d\n",timep->tm_year);
    printf("time->tm_mon: %d\n",timep->tm_mon);
    printf("time->tm_mday: %d\n",timep->tm_mday);
    printf("time->tm_hour: %d\n",timep->tm_hour);
    printf("time->tm_min: %d\n",timep->tm_min);
    printf("time->tm_sec: %d\n",timep->tm_sec);
// 本地时间
    localtime(&timestamp);  // 由于 timep 指向共享的 struct tm ，此处应该注意
    printf("time->tm_year: %d\n",timep->tm_year);
    printf("time->tm_mon: %d\n",timep->tm_mon);
    printf("time->tm_mday: %d\n",timep->tm_mday);
    printf("time->tm_hour: %d\n",timep->tm_hour);
    printf("time->tm_min: %d\n",timep->tm_min);
    printf("time->tm_sec: %d\n",timep->tm_sec);


// 初始化为 datetime 为格林尼治标准时
    DateTime datetime;
    createCalendarDateTime(&datetime);
    printf("currentCalendarDateTimeYear: %d\n",currentCalendarDateTimeYear(&datetime));
    printf("currentCalendarDateTimeYearMonth: %d\n",currentCalendarDateTimeYearMonth(&datetime));
    printf("currentCalendarDateTimeYearMonthDay: %d\n",currentCalendarDateTimeYearMonthDay(&datetime));
    printf("currentCalendarDateTimeYearMonthDayHour: %d\n",currentCalendarDateTimeYearMonthDayHour(&datetime));
    printf("currentCalendarDateTimeYearMonthDayMinute: %d\n",currentCalendarDateTimeYearMonthDayMinute(&datetime));
    printf("currentCalendarDateTimeYearMonthDaySecond: %d\n",currentCalendarDateTimeYearMonthDaySecond(&datetime));

// 初始化 datetime 为本地时
    createLocalDateTime(&datetime);
    printf("currentLocalDateTimeYear: %d\n",currentLocalDateTimeYear(&datetime));
    printf("currentLocalDateTimeYearMonth: %d\n",currentLocalDateTimeYearMonth(&datetime));
    printf("currentLocalDateTimeYearMonthDay: %d\n",currentLocalDateTimeYearMonthDay(&datetime));
    printf("currentLocalDateTimeYearMonthDayHour: %d\n",currentLocalDateTimeYearMonthDayHour(&datetime));
    printf("currentLocalDateTimeYearMonthDayMinute: %d\n",currentLocalDateTimeYearMonthDayMinute(&datetime));
    printf("currentLocalDateTimeYearMonthDaySecond: %d\n",currentLocalDateTimeYearMonthDaySecond(&datetime));


	return 0;
}
*/